﻿using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Text;

namespace TestJsonPRICH
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");

            TestReadConfigJson();


            //for (int i = 0; i < 3; i++)
            //{
            //    Console.WriteLine("输入Q退出：");
            //    ConsoleKeyInfo userInput = Console.ReadKey();
            //    if (userInput.Key!=ConsoleKey.Q )
            //    {
            //        TestJsonReadDI();
            //    }
            //}

            // 自定义类实现IConfigurationProvider 进行配置文件读取
            //TestMyselfConfig();
        }

        private static void TestMyselfConfig()
        {
            ServiceCollection services = new ServiceCollection();

            services.AddScoped<FxTestControl>();
            //1. 创建 ConfigurationBuilder
            ConfigurationBuilder cfgBuilder = new ConfigurationBuilder();

            //2. 添加Provider
            cfgBuilder.Add(new FxConfigSource() { Path = "web.config" });
            //上面可以演化成拓展方法
            //cfgBuilder.AddFxConfigSource("web.config");

            //3. 获取根节点
            IConfigurationRoot cfgRoot = cfgBuilder.Build();

            #region services 可以不要做到这一步就可以通过 cfgRoot 进行取值

            Console.WriteLine(cfgRoot["sqlConnectionString:name"]);
            Console.WriteLine(cfgRoot["sqlConnectionString:providerName"]);
            Console.WriteLine(cfgRoot["sqlConnection:value"]);
            Console.WriteLine(cfgRoot["sqlConnection:key"]);
            Console.WriteLine("+++++++++++++++++++++++");
            #endregion

            services.AddOptions().Configure<WebConfigEntity>(e => cfgRoot.Bind(e));

            using (var sp = services.BuildServiceProvider())
            {
                var fxCon = sp.GetRequiredService<FxTestControl>();
                fxCon.Test();
            }
        }

        /// <summary>
        /// 演示通过DI 服务注入来获取Json读取 演示
        /// </summary>
        private static void TestJsonReadDI()
        {
            ServiceCollection services = new ServiceCollection();

            IConfigurationBuilder cfgBuilder = new ConfigurationBuilder();
            cfgBuilder.AddJsonFile("config.json", true, true);
            IConfigurationRoot cfgRoot = cfgBuilder.Build();
            // 这句话需要 仔细观看 把根节点和People 进行绑定可以多个Configure 进行绑定
            services.AddOptions()
                .Configure<People>(c => cfgRoot.Bind(c))
                .Configure<Hobbys>(c => cfgRoot.GetSection("likes").Bind(c))
                ;

            services.AddScoped<PeopleControl>();

            using (ServiceProvider sp = services.BuildServiceProvider())
            {
                PeopleControl p = sp.GetRequiredService<PeopleControl>();
                p.TestName();
            }


        }

        private static void TestReadConfigJson()
        {
            //1. 构建对象
            ConfigurationBuilder cfgBuilder = new ConfigurationBuilder();

            //添加机密文件  添加顺序不同 覆盖效果也会不同， 这里具有后来者居上的问题
            cfgBuilder.AddUserSecrets<Program>();
            //2. 加载文件
            cfgBuilder.AddJsonFile("config.json", true, true);

            //3. 获取根节点
            IConfigurationRoot cfgRoot = cfgBuilder.Build();

            //4.获取数据
            string name = cfgRoot["name"];
            string age = cfgRoot["age"];

            Console.WriteLine($"名字：{name}\t 年龄：{age}\t 住址：{cfgRoot["address"]}");
            string firstLike = cfgRoot["likes:first"];
            string secondLike = cfgRoot["likes:second"];
            Console.WriteLine($"{firstLike}\t{secondLike}");

            //根据JSON获取 泛型类型
            People p = cfgRoot.Get<People>();
            Console.WriteLine(p.ToString());
        }
    }

    public class People
    {
        public string Name { get; set; }
        public int Age { get; set; }
        public string Address { get; set; }
        public Hobbys Likes { get; set; }
        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine($"名字：{this.Name}");
            sb.AppendLine($"年龄：{this.Age}");
            sb.AppendLine($"住址：{this.Address}");
            sb.AppendLine($"爱好：第一爱好：{this.Likes.First} \t 第二爱好：{Likes.Second}");
            return sb.ToString();
        }
    }

    public class Hobbys
    {
        public string First { get; set; }
        public string Second { get; set; }
    }
}
